#include "mainwindow.h"

MainWindow::MainWindow(QObject *parent) : QObject(parent),
    m_currentLayout(4),
    saveControl(nullptr)
{
    //连接设置界面的信号
    connect(&settingFunc, &SettingFunc::sigVideoChanged, this, &MainWindow::onDisplayCfgChanged);
    connect(&settingFunc, &SettingFunc::sigSetClick, this, &MainWindow::onResetConfig);

    //创建所有播放画面对象
    initDisplayer();

    //加载ini文件，保存到单个屏幕的配置对象中
    MyConfig m_cfg;
    QVector<WINDOW_CONFIG> configs = m_cfg.loadWindowConfigs();
    for(int i = 0; i < m_screens.size(); i++) {
        m_screenContainers[i]->m_windowConfig[0] = configs[0 + 4 * i];
        m_screenContainers[i]->m_windowConfig[1] = configs[1 + 4 * i];
        m_screenContainers[i]->m_windowConfig[2] = configs[2 + 4 * i];
        m_screenContainers[i]->m_windowConfig[3] = configs[3 + 4 * i];
    }

    settingFunc.configSettings(configs);   //获取ini文件内容，更新控件的值，传递信号到MainWindow

    //初始化存储功能
    initSave(configs, m_screens.size() * 4);

    for(int i = 0; i < m_screenContainers.size(); i++) {
        connect(m_screenContainers[i], &Displayer::sigSendDataToSave, this, &MainWindow::onSaveData);    //连接转存的数据
        connect(m_screenContainers[i], &Displayer::sigChangedVideoMode , [=](int index){
            settingFunc.changedVideoMode(i, index);
        });
        connect(m_screenContainers[i], &Displayer::sigSrcs, [=](int videoID, QString ipSrc){
            settingFunc.showIPInfo(i, videoID, ipSrc);  //显示器，视频编号，ip源
            settingFunc.showEncodeForm(i, videoID, "h264");
        });
        connect(m_screenContainers[i], &Displayer::sigVideoInfoChanged, [=](int videoID, int width, int height, int fps){
            settingFunc.showSolution(i, videoID, width, height);  //显示器，视频编号，ip源
            settingFunc.showFPS(i, videoID, fps);
            settingFunc.showEncodeForm(i, videoID, "h264");
        });

    }

    //开始接收并播放所有显示器的画面
    for(auto it : m_screenContainers) {
        it->initUI();
        it->startReceiving();
    }

    connect(&settingFunc, &SettingFunc::sigChangeDisplayMode, [=](int displayerID, int videoMode){
        m_screenContainers[displayerID]->onChangeViewMode(videoMode);
    });
}

MainWindow::~MainWindow()
{
    m_windowConfigs.append(m_windowConfig_1);
    m_windowConfigs.append(m_windowConfig_2);
    m_windowConfigs.append(m_windowConfig_3);
    m_windowConfigs.append(m_windowConfig_4);

    saveWindowConfigs.saveWindowConfigs(m_windowConfigs);

    for(auto it : m_screens) {
        it->deleteLater();
    }
    for(auto it : m_screenContainers) {
        it->deleteLater();
    }

    if(saveControl) {
        saveControl->deleteLater();
    }
}

void MainWindow::initDisplayer()
{
    //获取显示屏的数量，再进行初始化选择
    m_screens = QGuiApplication::screens();

    for(QScreen* screen : m_screens) {
        qDebug() << "Screen:" << screen->name()
                 << "Geometry:" << screen->geometry();
    }

    QDir appDir = (QCoreApplication::applicationDirPath());
    QString dir = QDir(appDir).filePath("config/videoMode.ini");
    QSettings settings(dir, QSettings::IniFormat);

    int videoMode[4] = {0};
    for(int i = 0; i < 4; i++) {
        videoMode[i] = settings.value(QString("VideoSettings/videoMode_%1").arg(i), 0).toInt();
    }

    //根据显示器个数创建对象
    for(int i = 0; i < m_screens.size(); i++) {
        //界面初始化
        Displayer *window = new Displayer(videoMode[i]);
        connect(window, &Displayer::sigExit, qApp, &QApplication::quit);  //退出整个应用程序
        connect(window, &Displayer::sigOpenSettings, this, &MainWindow::onOpenSettingForm); //打开设置界面

        window->setWindowTitle(QString("显示器 %1").arg(m_screens[i]->name()));
        window->setGeometry(m_screens[i]->geometry()); //设置对应显示器的界面并加入容器
        window->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
        //window->showFullScreen();
        window->showMaximized();
        //window->startReceiving();

        m_screenContainers.append(window);
    }
}

void MainWindow::onOpenSettingForm()
{
    settingFunc.hide();
    settingFunc.show();
}

void MainWindow::onResetConfig(int displayerID, int videoID)
{
    m_screenContainers[displayerID]->resetSettings(videoID);
}

void MainWindow::onDisplayCfgChanged(quint8 displayNum, WINDOW_CONFIG config)
{
    if(displayNum >= m_screens.size()) return; //如果大于屏幕个数就直接退出

    switch (displayNum) {
    case 0: {
        m_windowConfig_1[config.videoNum].videoNum = config.videoNum;
        m_windowConfig_1[config.videoNum].resourcesName = config.resourcesName;
        m_windowConfig_1[config.videoNum].ip = config.ip;
        m_windowConfig_1[config.videoNum].port = config.port;
        m_windowConfig_1[config.videoNum].netCard = config.netCard;

        if(m_screens.size() > 1) {
            m_screenContainers[0]->m_windowConfig[config.videoNum] = m_windowConfig_1[config.videoNum];
        }

        break;
        }
    case 1: {
        m_windowConfig_2[config.videoNum].videoNum = config.videoNum;
        m_windowConfig_2[config.videoNum].resourcesName = config.resourcesName;
        m_windowConfig_2[config.videoNum].ip =  config.ip;
        m_windowConfig_2[config.videoNum].port =  config.port;
        m_windowConfig_2[config.videoNum].netCard =  config.netCard;

        if(m_screens.size() > 2) {
            m_screenContainers[1]->m_windowConfig[config.videoNum] = m_windowConfig_2[config.videoNum];
        }

        break;
        }
    case 2: {
        m_windowConfig_3[config.videoNum].videoNum = config.videoNum;
        m_windowConfig_3[config.videoNum].resourcesName = config.resourcesName;
        m_windowConfig_3[config.videoNum].ip = config.ip;
        m_windowConfig_3[config.videoNum].port = config.port;
        m_windowConfig_3[config.videoNum].netCard = config.netCard;

        if(m_screens.size() > 2) {
            m_screenContainers[2]->m_windowConfig[config.videoNum] = m_windowConfig_3[config.videoNum];
        }

        break;
        }
    case 3: {
        m_windowConfig_4[config.videoNum].videoNum = config.videoNum;
        m_windowConfig_4[config.videoNum].resourcesName = config.resourcesName;
        m_windowConfig_4[config.videoNum].ip = config.ip;
        m_windowConfig_4[config.videoNum].port = config.port;
        m_windowConfig_4[config.videoNum].netCard = config.netCard;

        if(m_screens.size() > 3) {
            m_screenContainers[3]->m_windowConfig[config.videoNum] = m_windowConfig_4[config.videoNum];
        }
        break;
        }
    default:
        break;
    }
}

void MainWindow::onSaveData(int videoID, const QByteArray &data, QString ipSrc, quint16 portSrc)
{
    emit sigSendToSave(ipSrc, portSrc, data);   //存储暂时只需要这两个参数，后续看需求删除不必要的参数
}

void MainWindow::initSave(QVector<WINDOW_CONFIG>& configs, int validNum)
{
    qDebug() << "start save...";

    saveControl = new MainControl(settingFunc.getSettingsData());
    //连接存储信号
    connect(this, &MainWindow::sigSendToSave, saveControl, &MainControl::signal_saveFileWork);
    connect(&settingFunc, &SettingFunc::sigSettingsData, saveControl, &MainControl::onSettingsData);

    saveControl->setConfig(configs, validNum);
    saveControl->initThread();
    saveControl->startThread();
}
